package GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class LoginGUI implements ActionListener {
    private JTextField id;
    private JPasswordField pw;
    private JRadioButton radio_adm, radio_tea, radio_stu;

    final String adm_id = "111";//管理员账号
    final String adm_pw = "111";//管理员密码
    Font f = new Font("宋体", Font.BOLD, 16);
    JFrame jf = new JFrame("考试管理系统---登陆界面");

    public LoginGUI() {
        JPanel jp_id = new JPanel();
        JLabel jl_id = new JLabel("账号:");
        id = new JTextField(18);
        jl_id.setFont(f);
        jp_id.add(jl_id);
        jp_id.add(id);

        JPanel jp_pw = new JPanel();
        JLabel jl_pw = new JLabel("密码:");
        pw = new JPasswordField(18);
        jl_pw.setFont(f);
        jp_pw.add(jl_pw);
        jp_pw.add(pw);

        JPanel radio = new JPanel();
        radio_adm = new JRadioButton("教务员");
        radio_tea = new JRadioButton("教师");
        radio_stu = new JRadioButton("学生");
        ButtonGroup group = new ButtonGroup();
        group.add(radio_adm);
        group.add(radio_tea);
        group.add(radio_stu);
        radio.add(radio_adm);
        radio.add(radio_tea);
        radio.add(radio_stu);
        radio_adm.addActionListener(this);
        radio_tea.addActionListener(this);
        radio_stu.addActionListener(this);

        JPanel login = new JPanel();
        JButton jb_login = new JButton("登录");
        jb_login.setFont(f);
        jb_login.addActionListener(this);
        login.add(jb_login);

        JPanel jp = new JPanel();
        jp.setLayout(new GridLayout(4, 1));
        jp.add(jp_id);
        jp.add(jp_pw);
        jp.add(radio);
        jp.add(login);

        jf.add(jp, BorderLayout.CENTER);
        jf.setResizable(false);//固定窗口不可变
        //jf.setLocationRelativeTo(null);//窗口居中
        jf.setLocation(560, 270);
        jf.setVisible(true);//可见
        jf.setSize(450, 300);//大小
        jf.setDefaultCloseOperation(jf.DISPOSE_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand()=="登录") {
            if(radio_adm.isSelected()) {
                String _id = id.getText();
                String _pw = String.valueOf(pw.getPassword());//函数返回指定对象的原始值。
                if (_id.equals(adm_id)&&_pw.equals(adm_pw)){
                    new admUI();
                }else{
                    JOptionPane.showMessageDialog(null,"用户名或者密码输入为空或错误！请重新输入");
                }
            }
            else if(radio_tea.isSelected()) {
                teaLogin();
            }
            else if(radio_stu.isSelected()) {
                stuLogin();
            }
        }
    }
    public void teaLogin(){
        String _id = id.getText();
        String _pw = String.valueOf(pw.getPassword());//函数返回指定对象的原始值即密码
        if(_id.isEmpty() && _pw.isEmpty()){
            JOptionPane.showMessageDialog(null,"用户名或者密码输入为空或错误！请重新输入");
        }
    }
    public void stuLogin(){
        String _id = id.getText();
        String _pw = String.valueOf(pw.getPassword());
        if(_id.isEmpty() && _pw.isEmpty()){
            JOptionPane.showMessageDialog(null,"用户名或者密码输入为空或错误！请重新输入");
        }
    }

}
